package com.example.lottery.controller;
import java.util.Date;

import com.example.lottery.common.pojo.CommonResult;
import com.example.lottery.common.utils.JacksonUtil;
import com.example.lottery.controller.param.DrawPrizeParam;
import com.example.lottery.controller.param.ShowWinningRecordsParam;
import com.example.lottery.controller.result.WinningRecordResult;
import com.example.lottery.dao.datobject.WinningRecordDO;
import com.example.lottery.service.DrawPrizeService;
import com.example.lottery.service.dto.WinningRecordDTO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class DrawPrizeController {
    @Autowired
    private DrawPrizeService drawPrizeService;
    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@Valid @RequestBody DrawPrizeParam param){
        log.info("drawPrize DrawPrizeParam:{}", JacksonUtil.writeValueAsString(param));
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }
    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(
            @Valid @RequestBody ShowWinningRecordsParam param){
        log.info("showWinningRecords ShowWinningRecordsParam",JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTOList=drawPrizeService.getRecords(param);
        return CommonResult.success(
                convertToWinningRecordResultList(winningRecordDTOList));
    }
    private List<WinningRecordResult> convertToWinningRecordResultList(List<WinningRecordDTO> winningRecordDOList) {
        if(CollectionUtils.isEmpty(winningRecordDOList)){
            //返回空列表
            return Arrays.asList();
        }
        return winningRecordDOList.stream()
                .map(winningRecordDTO->{
                    WinningRecordResult winningRecordResult=new WinningRecordResult();
                    winningRecordResult.setWinnerId(winningRecordDTO.getWinnerId());
                    winningRecordResult.setWinnerName(winningRecordDTO.getWinnerName());
                    winningRecordResult.setPrizeName(winningRecordDTO.getPrizeName());
                    winningRecordResult.setPrizeTier(winningRecordDTO.getPrizeTier().getMessage());
                    winningRecordResult.setWinningTime(winningRecordDTO.getWinningTime());
                    return winningRecordResult;
                }).collect(Collectors.toList());

    }
}
